//Komissarov Denis
package GUI;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class CLED {

	ArrayList<CLEDItem> itemsArray = new ArrayList<CLEDItem>(); // Array of LED
																// items from
																// CLEDItem
																// class
	private BufferedImage image; // image to export from class
	private int height; // total height
	private int width; // total width
	private int itemWidth; // item width

	// constructors
	public CLED() {

	}

	public CLED(int size, Image segment, Image dot) {

		initializing(size, segment, dot);

	}

	private void initializing(int size, Image segment, Image dot) {

		if (size == 0 || segment == null || dot == null)
			return;
		itemsArray = new ArrayList<CLEDItem>();

		for (int index = 0; index < size; index++) {
			itemsArray.add(new CLEDItem(segment, dot, ' ', false));
		}

		height = itemsArray.get(0).getHeight();
		itemWidth = itemsArray.get(0).getWidth();
		width = size * itemWidth;

		// Create background image
		image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
	}

	public BufferedImage clearImage(BufferedImage bufImg) {
		for (int x = 0; x < bufImg.getWidth(); x++) {
			for (int y = 0; y < bufImg.getHeight(); y++) {
				bufImg.setRGB(x, y, (0xFF));
			}
		}
		return bufImg;
	}

	// Drawing items on the image
	private Image draw() {

		image = clearImage(image);
		Graphics2D canv = image.createGraphics();
		canv.setBackground(new Color(0, 0, 0, 0));

		for (int index = 0; index < itemsArray.size(); index++) {
			canv.drawImage(itemsArray.get(index).drawItem(), itemWidth * index,
					0, null);
		}

		canv.dispose();
		return image;
	}

	// String to LED
	public Image StringToLED(String LEDText, boolean progMode) {

		if (LEDText.length() < itemsArray.size())
			while (itemsArray.size() != LEDText.length())
				LEDText = LEDText + " ";

		char[] letters = LEDText.toCharArray();
		char letter;
		boolean comma = false;
		int offset = 0;

		if (!progMode) {

			for (int index = 0; index < itemsArray.size(); index++) {

				// Working with symbols
				comma = false;
				if (index + offset + 1 < LEDText.length()) {
					if (letters[index + offset + 1] == '.'
							|| letters[index + offset + 1] == ',') {
						comma = true;
					}
				}

				// if (comma == true && (index + offset + 2 < LEDText.length()))
				// {
				// if (letters[index + offset + 2] == 'E'
				// || letters[index + offset + 2] == 'e')
				// comma = false;
				// }

				// Adding spaces if out of range
				if (index + offset >= LEDText.length())
					letter = ' ';
				else
					letter = letters[index + offset];

				// Set the spaces to the places of some symbols
				if (letter == '+')
					letter = ' ';

				// Shifting all after E
				if (letter == 'E') {
					letter = ' ';
					for (int i = index + offset; i < itemsArray.size() - 4; i++) {
						letters[i + 4] = letters[i + 3];
						letters[i + 3] = letters[i + 2];
						letters[i + 2] = letters[i + 1];
						letters[i + 1] = letters[i];
						letters[i] = ' ';
					}
				}

				if (comma == true)
					offset++;
				itemsArray.get(index).setLetter(letter, comma);
			}
		} else {
			comma = false;
			for (int index = 0; index < itemsArray.size(); index++) {

				// Working with symbols

				// Adding spaces if out of range
				if (index  >= LEDText.length())
					letter = ' ';
				else
					letter = letters[index];

				// Set the spaces to the places of some symbols
				if (letter == '+')
					letter = ' ';
			
				itemsArray.get(index).setLetter(letter, comma);
				
			}

		}

		return draw();
	}

}
